home *** CD-ROM | disk | FTP | other *** search
- Path: news.sdm.de!wagner
- From: wagner@sdm.de (Dr. Klaus Wagner)
- Newsgroups: comp.object,comp.lang.c++
- Subject: Re: Defining Object Associations
- Followup-To: comp.object,comp.lang.c++
- Date: 28 Mar 1996 14:30:47 GMT
- Organization: sd&m GmbH & Co. KG, Munich, Germany
- Message-ID: <4je7qn$hlr@sunti1.sdm.de>
- References: <4j9ens$jtq@crchh327.rich.bnr.ca>
- Reply-To: wagner@sdm.de
- NNTP-Posting-Host: sunfi1.fi.sdm.de
- X-Newsreader: TIN [version 1.2 PL2]
-
- In <4j9ens$jtq@crchh327.rich.bnr.ca>, Joseph wrote:
-
- > For example, a LoggingManager object and DisplayManager object
- > must be most constructed before creating a CommandDispatcher.
- > Yet the CommandDispatcher also requires that an EventManager
- > has been created, etc.
-
- Although you didn't say it explicitly, it semms to me as
- the Objects you are dealing with are mostly uniq in
- your system. They are *singletons*. aren't they?
-
- If this is true and you dont know about singletons,
- see the patterns literature for the singleton pattern.
-
-
- > So I have code that goes along creating these objects, and
- > when I create the CommandDispatcher I say,
-
- > CommandDispatcher* CD = new CommandDispatcher(LM, DM, EM);
-
- > etc.
-
- One way to solve this, if you do have singletons is to
- use static lazy access methods for the singletons which
- your constructors call on initialization.
-
- Looks like this:
-
- class LM {
- public: LM * get_the_LM() {
- static LM lm;
- return &lm;
- }
- private: LM(...) {...}
- };
- class DM ... etc ...
-
- class CommandDispatcher {
- public:
- CommandDispatcher()
- : pLM( LM::get_the_LM() ),
- pDM( DM::get_the_DM() ),
- pEM( EM::get_the_EM() )
- { .. do your stuff with *pLM etc }
-
- private:
- LM * pLM;
- ....
- };
-
- Doing so, you still have to add it to the Ctor-implementation,
- but not to the interface, which hinderes changes to
- propagate wildly through your application code.
- And you dont any longer have to care about the order of
- construction of those singeltons. The little clause 'static'
- in the accesors of the singletons just handle all this for
- you.
-
- Provided that there are no cycles in this process of
- propagating construktion of singletons.
-
- If you do have cycles, you are lost anyway.
- But changing the above code only slightly can help you
- to detect this kind of loops automaticley at runtime.
- I know this is a very unsatisfactory promise, but anything
- else you have to figer out your self by looking at your object
- modell.
-
- > But whenever the CommandDispatcher needs to talk a different
- > type of object, I have to add it to the constructor and also
- > ensure that the new object has all of its requirements
- > fulfilled before created the CommandDispatcher.
-
- > This presents a headache because I have to worry about the
- > order in which I create all of these things, and my code
- > is rearranged a lot.
-
- > So what I would like is a global container of references to
- > pointers that I can attach to each object as I declare its
- > pointer (before I new it). Then when I'm ready to new it
- > I pass in all of the references (via a single struct or
- > some sort) and everybody knows about everybody else.
-
-
- So, in essence, the above proposal *is* using a global container.
- The container is managed by the compiler in the static data area
- and you don't have to care about layout and initialization.
- But you hide it behind some accessor and so you have allways the
- freedon to rethink and change this decission.
-
-
- Hope this helps
-
- --
-
- Gruss - Klaus
-
-
- Klaus Wagner |s |d &|m | software design & management, Ltd.
- | | | | Thomas-Dehler-Str. 27 (division FI)
- | | | | 81737 Munich, Germany
- Tel:+49-89-63812-333 | | | | Fax.: +49-89-63812-490
- +49-89-92446272(hypo)| | | | Internet: wagner@sdm.de
-